package com.personal.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.personal.pojo.TApply;
import com.personal.pojo.TCoach;
import com.personal.pojo.TCourse;
import com.personal.pojo.TMember;
import com.personal.service.*;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/coach")
public class CoachController {
    //设置每页显示数据量
    private static final int PAGE_SIZE = 10;
    @Autowired
    private CoachService coachService;

//1.coach登录
    @RequestMapping("/login")
    @ResponseBody
    public String login(String username, String password, HttpSession session,Model model){
        TCoach coach = coachService.login(username,password);
        System.out.println("进入coachcontroller");
        if (coach != null){
            //教练员登录
            session.setAttribute("logUser",coach);
            session.setAttribute("logUsername",coach.getCoachrealname());
            return "ok_co";
        }else {
            model.addAttribute("msg","您不是教练员！");
            return "no";
        }
    }

    //1.1登录信息验证成功跳转页面
    @RequestMapping("/toIndex")
    public String toIndex(){

        return "index";
    }

//2.课程模块，跳转到课程列表,将课程信息附带上,分页显示
    @RequestMapping("/toCoursePage")
    public String toCoursePage(Model model){
        PageInfo info = coachService.findNotEnd(1,PAGE_SIZE);
        model.addAttribute("courseInfo",info);
        return "coachcourse_list";
    }
    @RequestMapping("/CourseSplit")
    @ResponseBody
    public void CourseSplit(int page,HttpSession session){
        PageInfo info = coachService.findNotEnd(page,PAGE_SIZE);
        session.setAttribute("courseInfo",info);
    }
    //2.1教练多条件查询课程,分页显示
    @RequestMapping("/coachfindcourse")
    @ResponseBody
    public void coachfindcourse(Integer weekid, Integer sectionid, String coursename,HttpSession session){
        PageInfo info = coachService.findByTimeName(1,PAGE_SIZE,weekid,sectionid,coursename);
        session.setAttribute("courseInfo",info);
    }
    //2.1教练多条件查询课程，分页
    @RequestMapping("/findcourseSplit")
    @ResponseBody
    public void findcourseSplit(int page,Integer weekid, Integer sectionid, String coursename,HttpSession session){
        PageInfo info = coachService.findByTimeName(page,PAGE_SIZE,weekid,sectionid,coursename);
        session.setAttribute("courseInfo",info);
    }
//3.教室模块，跳转到教室列表,将教室信息附带上
    @RequestMapping("/toClassroomPage")
    public String toClassroomPage(Model model){
        PageInfo info = coachService.findClassroom(1,PAGE_SIZE);
        model.addAttribute("classroomInfo",info);
        return "coachclassroom_list";
    }
    //分页
    @RequestMapping("/ClassroomSplit")
    @ResponseBody
    public void ClassroomSplit(int page,HttpSession session){
        PageInfo info = coachService.findClassroom(page,PAGE_SIZE);
        session.setAttribute("classroomInfo",info);
    }
    //3.1教室多条件查询
    @RequestMapping("/coachFindClassroom")
    @ResponseBody
    public void selectClassroom(String classroomtype, Integer min, Integer max, String classroomName,HttpSession session){
        PageInfo info = coachService.findByCluase(1,PAGE_SIZE,classroomtype,min,max,classroomName);
        session.setAttribute("classroomInfo",info);
    }
    //3.1教室多条件查询,分页
    @RequestMapping("/findClSplit")
    @ResponseBody
    public void findClSplit(int page,String classroomtype, Integer min, Integer max, String classroomName,HttpSession session){
        PageInfo info = coachService.findByCluase(page,PAGE_SIZE,classroomtype,min,max,classroomName);
        session.setAttribute("classroomInfo",info);
    }
    //4.跳转个人中心页
    @RequestMapping("/toOneselfPage")
    public String toOneselfPage(Model model,HttpSession session){
        //获取登录session
        TCoach coach = (TCoach) session.getAttribute("logUser");
        //将学员预约情况和任教课程情况带上
        List list = coachService.findAllMem(Integer.valueOf(coach.getCoachid()));
        model.addAttribute("resMem",list);
        return "coachOneself";
    }
    //删除到期的预约会员
    @RequestMapping("/delMem")
    @ResponseBody
    public String delMem(Integer reserveid){
        //根据id删除记录
        int row = coachService.deleteMem(reserveid);
        if (row==1){
            return "ok";
        }else {
            return "no";
        }
    }
    //4.1个人中心：获取登录用户预约的所有教练,根据用户id来查,弃用
    @RequestMapping("/getAllMem")
    @ResponseBody
    public String getAllCoach(String coachid){
        //1.查询用户教练预约情况，从reserve表中查
        List list = coachService.findAllMem(Integer.valueOf(coachid));
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String json = objectMapper.writeValueAsString(list);
            return json;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
    //4.2修改个人信息
    @RequestMapping("/editMess")
    @ResponseBody
    public String editMess(String coachid,String coachlogname,String coachpassword, String coachrealname, String coachsex, String cotype,HttpSession session){
        TCoach coach = new TCoach();
        coach.setCoachid(Integer.valueOf(coachid));
        coach.setCoachLogName(coachlogname);
        coach.setCoachpassword(coachpassword);
        coach.setCoachrealname(coachrealname);
        coach.setCoachsex(coachsex);
        coach.setCotype(cotype);
        int row = coachService.editOneself(coach);
        if (row == 1){
            //查询coach
            TCoach coach1 = coachService.findBycoachId(Integer.valueOf(coachid));
            //更新session内容
            session.setAttribute("logUser",coach1);
            return "ok";
        }
        return "no";
    }
    // 4.3跳转到登录用户个人 课程表 页面
    @RequestMapping("/toCoachcourseSchedule")
    public String toCoachcourseSchedule(Model model,HttpSession session){
        //获取session内容，也就是登录的用户信息
        TCoach coach = (TCoach) session.getAttribute("logUser");
        //1.从课程表中查询所有任教的课程
        List<TCourse> list = coachService.findByCoachid(coach.getCoachid());
        //遍历将报名表中的课程信息，赋予相应的位置str=a11：代表第一节 星期一。
        list.forEach(course -> {
            //获取星期几
            String strWeek = String.valueOf(course.getWeek().getWeekid());
            //获取第几节课
            String strSection = String.valueOf(course.getSection().getSectionid());
            //第几节+第几周
            String str ="a"+strSection+strWeek;
            model.addAttribute(str,course);
        });
        return "coachcourse_schedule";
    }
}
